package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.HisHospitalRecordsDto;
import com.woniu.dto.PageDTO;
import com.woniu.exception.DIYException;
import com.woniu.model.HisHospitalRecords;
import com.woniu.mapper.HisHospitalRecordsMapper;
import com.woniu.param.HisHospitalRecordsAddParam;
import com.woniu.param.HisHospitalRecordsParam;
import com.woniu.param.PageParam;
import com.woniu.service.IHisHospitalRecordsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.CopyBeanUtil;
import com.woniu.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-11
 */
@Service
public class HisHospitalRecordsServiceImpl extends ServiceImpl<HisHospitalRecordsMapper, HisHospitalRecords> implements IHisHospitalRecordsService {
    @Resource
    private HisHospitalRecordsMapper hisHospitalRecordsMapper;


    @Override
    public Result<PageDTO<HisHospitalRecordsDto>> getAllHisHospitalRecords() {
        return null;
    }

    @Override
    public Result<PageDTO<HisHospitalRecordsDto>> getHisHospitalRecordsBy(HisHospitalRecordsParam hisHospitalRecordsParam) {
        if (hisHospitalRecordsParam==null){new DIYException(-9999,"hisHospitalRecordsParam为空");}
        QueryWrapper<HisHospitalRecords> queryWrapper = new QueryWrapper<>();
        if (hisHospitalRecordsParam.getHospitalRecordsId()!=null){
            queryWrapper.eq("hospital_records_id",hisHospitalRecordsParam.getHospitalRecordsId());
        }
        if (hisHospitalRecordsParam.getPatientId()!=null){
            System.out.println(1);
            queryWrapper.eq("patient_id",hisHospitalRecordsParam.getPatientId());
        }
        if (hisHospitalRecordsParam.getPatientName()!=null){
            queryWrapper.eq("patient_name",hisHospitalRecordsParam.getPatientName());
        }
        if (hisHospitalRecordsParam.getOfficeId()!=null){
            queryWrapper.eq("office_id",hisHospitalRecordsParam.getOfficeId());
        }
        if (hisHospitalRecordsParam.getOfficeName()!=null){
            queryWrapper.eq("office_name",hisHospitalRecordsParam.getOfficeName());
        }
        if (hisHospitalRecordsParam.getDoctorId()!=null){
            queryWrapper.eq("doctor_id",hisHospitalRecordsParam.getDoctorId());
        }
        if (hisHospitalRecordsParam.getDoctorName()!=null){
            queryWrapper.eq("doctor_name",hisHospitalRecordsParam.getDoctorName());
        }
        if (hisHospitalRecordsParam.getRoomId()!=null){
            queryWrapper.eq("room_id",hisHospitalRecordsParam.getRoomId());
        }
        if (hisHospitalRecordsParam.getRoomNumber()!=null){
            queryWrapper.eq("room_number",hisHospitalRecordsParam.getRoomNumber());
        }
        if (hisHospitalRecordsParam.getBedId()!=null){
            queryWrapper.eq("bed_id",hisHospitalRecordsParam.getBedId());
        }
        if (hisHospitalRecordsParam.getBedNum()!=null){
            queryWrapper.eq("bed_num",hisHospitalRecordsParam.getBedNum());
        }
        if (hisHospitalRecordsParam.getHospitalTime()!=null){
            queryWrapper.eq("hospital_time",hisHospitalRecordsParam.getHospitalTime());
        }
        if (hisHospitalRecordsParam.getHospitalLeave()!=null){
            queryWrapper.eq("hospital_leave",hisHospitalRecordsParam.getHospitalLeave());
        }
        if (hisHospitalRecordsParam.getHospitalStatus()!=null){
            System.out.println(2);
            queryWrapper.eq("hospital_status",hisHospitalRecordsParam.getHospitalStatus());
        }
        if(hisHospitalRecordsParam.getPageParam()==null){
            PageParam pageParam = new PageParam();
            pageParam.setPage(0L);
            pageParam.setPageSize(100L);
            hisHospitalRecordsParam.setPageParam(pageParam);
        }
        List<HisHospitalRecords> hisHospitalRecords = hisHospitalRecordsMapper.selectList(queryWrapper);
        System.out.println(hisHospitalRecords);
        Page<HisHospitalRecords> page = page(new Page<HisHospitalRecords>(hisHospitalRecordsParam.getPageParam().getPage(),hisHospitalRecordsParam.getPageParam().getPageSize()),queryWrapper);
        List<HisHospitalRecords> records = page.getRecords();
        List<HisHospitalRecordsDto> hisHospitalRecordsDtos = CopyBeanUtil.copyBean(records, HisHospitalRecordsDto.class);
        return Result.bulid(PageDTO.bulid(page,hisHospitalRecordsDtos));
    }

    @Override
    public Result<HisHospitalRecordsDto> updateHisHospitalRecords(HisHospitalRecordsParam hisHospitalRecordsParam) {
        if (hisHospitalRecordsParam==null)new DIYException(-9999,"服务器异常，hisHospitalRecordsParam为空");
        HisHospitalRecords hisHospitalRecords = new HisHospitalRecords();
        HisHospitalRecordsDto hisHospitalRecordsDto = new HisHospitalRecordsDto();
        BeanUtils.copyProperties(hisHospitalRecordsParam,hisHospitalRecords);
        BeanUtils.copyProperties(hisHospitalRecordsParam,hisHospitalRecordsDto);
        hisHospitalRecordsMapper.updateById(hisHospitalRecords);
        return Result.bulid(hisHospitalRecordsDto);
    }

    @Override
    public Result<HisHospitalRecordsDto> addHisHospitalRecords(HisHospitalRecordsAddParam hisHospitalRecordsAddParam) {
        if (hisHospitalRecordsAddParam==null) new DIYException(-9999,"服务器异常，hisHospitalRecordsAddParam为空");
        HisHospitalRecords hisHospitalRecords = new HisHospitalRecords();
        BeanUtils.copyProperties(hisHospitalRecordsAddParam,hisHospitalRecords);
        hisHospitalRecordsMapper.insert(hisHospitalRecords);
        HisHospitalRecordsDto hisHospitalRecordsDto = new HisHospitalRecordsDto();
        BeanUtils.copyProperties(hisHospitalRecords,hisHospitalRecordsDto);
        return Result.bulid(hisHospitalRecordsDto);
    }
}
